package com.bw.zcb.mvp.model;

import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.model.BaseModel;
import com.bw.zcb.mvp.contract.PersonContract;
import com.bw.zcb.mvp.model.entity.UploadEntity;
import com.bw.zcb.network.tool.Api;
import com.bw.zcb.network.tool.ChangeFunction;
import com.bw.zcb.network.tool.HttpFactory;
import com.bw.zcb.network.tool.HttpType;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class PersonModel extends BaseModel implements PersonContract.IPersonModel {
    @Inject
    public PersonModel(){}
    @Override
    public Observable<BaseEntity> requestUpdate(Map<String, Object> map) {
        int code = (int) map.get("code");
        List<MultipartBody.Part> list = (List<MultipartBody.Part>) map.get("list");
        map.remove("list");
        map.remove("code");
        if (code== HttpCode.UPDATEUSERCODE){
            RequestBody body = createBody(map);
            Observable<BaseEntity> ob = HttpFactory.getInstance()
                    .factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit().create(Api.class).requestUpdateUser(body);
            return  ob.map(new ChangeFunction<BaseEntity>());
        }else if (code==HttpCode.UPLOADIMGCODE){
            Observable<UploadEntity> ob = HttpFactory.getInstance().factory(HttpType.UPLOADTYPE)
                    .getRetrofit().create(Api.class)
                    .requestUploadHead(list);
            return ob.map(new ChangeFunction<UploadEntity>());
        }
        return null;
    }
}
